package com.wtcrazy.controller;

import com.wtcrazy.dao.ArticleMapper;
import com.wtcrazy.entity.Article;
import com.wtcrazy.service.IArticleService;
import com.wtcrazy.service.Impl.ArticleServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleServiceImpl articleService;

    @PostMapping("UploadArticle")
    public Boolean saveArticle(@RequestBody Article article) {
        return articleService.saveArticle(article);
    }

    @GetMapping("/FindAll")
    public List<Article> findAll() {
        return articleService.list();
    }

    @GetMapping("/Page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(defaultValue = "") String title) {
        pageNum = (pageNum - 1) * pageSize;
        List<Article> data = articleMapper.selectPage(pageNum, pageSize, title);
        Integer total = articleMapper.selectTotal(title);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    @ApiOperation("删除文章")
    @DeleteMapping("/DeleteArticle")//
    public String deleteArticle(@RequestParam String id) {
        return articleService.deleteArticle(id);
    }
}
